package com.imooc.ad.controller;

import com.alibaba.fastjson.JSON;
import com.imooc.ad.annotation.IgnoreResponseAdvice;
import com.imooc.ad.client.SponsorClient;
import com.imooc.ad.client.vo.AdPlan;
import com.imooc.ad.client.vo.AdPlanGetRequest;
import com.imooc.ad.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @Description: TODO
 * @ClassName: SearchController
 * @Author: zhangzhongyu
 * @Date: 2019-10-14 20:57
 * @Version: 1.0
 */
@Slf4j
@RestController
public class SearchController {

    private final RestTemplate restTemplate;

//    private final SponsorClient sponsorClient;

    @Autowired
    public SearchController(RestTemplate restTemplate) {
        this.restTemplate = restTemplate;
//        this.sponsorClient = sponsorClient;
    }

//    @IgnoreResponseAdvice
//    @PostMapping("/getAdPlans")
//    public CommonResponse<List<AdPlan>> getAdPlans(@RequestBody AdPlanGetRequest request) {
//        log.info("ad-search: getAdPlans -> {}", JSON.toJSONString(request));
//        return sponsorClient.getAdPlans(request);
//    }

    @SuppressWarnings("all")
    @IgnoreResponseAdvice       // 标记这个注解是因为不想得到统一的响应
    @PostMapping("/getAdPlansByRibbon")
    public CommonResponse<List<AdPlan>> getAdPlanByRibbon(
            @RequestBody AdPlanGetRequest request) {
        log.info("ad-search: getAdPlanByRibbon -> {}", JSON.toJSONString(request));
        return restTemplate.postForEntity(
            "http://eureka-client-ad-sponsor/ad-sponsor/get/adPlan",
                request,
                CommonResponse.class
        ).getBody();
    }
}
